var util = require('../../utils/util.js')
var app = getApp();

var reload = true;
var buy_hasMore = true;
var buy_loadingMore = false;

var page = 1;
var tc = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    util: util,
    tc: 0,

    buy_orders: [],
    hasBuyData:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tc = app.get_tc();
    var titlename = tc == 1 ? '掃碼記錄' : '扫码记录';
    wx.setNavigationBarTitle({
      title: titlename,
    })
    
    this.getScanRecordData(1, 20);

    var that = this;
    
    wx.getSystemInfo({
      success: function (res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
          tc: tc
        });
      }
    });

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });

    page = 1;
    
  },

  loadMore: function (e) {
    if (buy_hasMore && !buy_loadingMore) {
        buy_loadingMore = true;
        page++;
      this.getScanRecordData(page, 20);
      } else {
        console.log("没有更多了");
      }
  },


  
  getScanRecordData: function (page, num) {
    wx.showLoading({
      title: '加载中',
      mask: 'true'
    })
    var that = this;
    var params = {
      "ac": "getcdbsaoma",
      "publickey": util.publiccode,
      "page": page,
      "num": num,
    };
    util.api({
      data: params,
      success: function (responseData) {
        console.log(responseData);
        if (!responseData.data.msg) {
          console.log(responseData.data);
          var result = responseData.data;
          result.forEach(function(m, i){
            m.time = that.toDate(parseInt(m.createtime));
            let start = m.guiid.length - 4;
            m.showguiid = "ID: ******" + m.guiid.substr(start, 4)
          });
          if (that.data.buy_orders.length > 0) {
            result = that.data.buy_orders.concat(responseData.data);
          }
          console.log('result:',result)
          that.setData({
            buy_orders: result
          });
        } else {
          //买入
          buy_hasMore = false;
          if (that.data.buy_orders.length <= 0) {
            that.setData({
              hasBuyData: false
            });
          }
        }
        //买入
        buy_loadingMore = false;
        wx.hideLoading();
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  goAtherPage: function (e) {
    // var url = e.currentTarget.dataset.link;    
    // wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  
  //时间戳转换时间  
  toDate: function (number) {
    var n = number * 1000;
    var date = new Date(n);
    var Y = date.getFullYear() + '.';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '.';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var H = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    console.log(Y + M + D + "  " + H + ':' + m)
    return (Y + M + D + "  " + H + ':' + m)

  }

})